home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Behaviors / Actions / Change Property.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  13.0 KB  |  366 lines

  1. // Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBAL VARS  *****************
  4.  
  5. var helpDoc = MM.HELP_behChangeProperty;
  6.  
  7. function initGlobals() {
  8.   var x,y;
  9.  
  10.   TAGTYPES = new Array("LAYER","DIV","SPAN","IMG","FORM","INPUT/CHECKBOX","INPUT/RADIO",
  11.                          "INPUT/TEXT","TEXTAREA","INPUT/PASSWORD","SELECT");
  12.   MENUMAXLEN = 0;
  13.  
  14.   PROP = new Array();
  15.   //Create array for each tagname, which we can access with PROP[browser][tag]
  16.   for (x=0; x<BROWSERS.length; x++) {
  17.     PROP[x] = new Array();
  18.     for (y=0; y<TAGNAMES.length; y++) PROP[x][y] = new Array();
  19.   }
  20.  
  21.   //This table provides a dynamic way add properties to the picklist. There are are 4
  22.   //groups, one for each browser/version (see BROWSERS array above).
  23.   //You can add writeable properties, comma-separated, between the first pair of quotes.
  24.   //To add another tag, extend the TAGNAMES and TAGTYPES arrays above, then add another
  25.   //array element to each group below.
  26.  
  27.   //Netscape 3 Writable Properties
  28.   PROP[0][ 0] = "".split(",");//layer
  29.   PROP[0][ 1] = "".split(",");//div
  30.   PROP[0][ 2] = "".split(",");//span
  31.   PROP[0][ 3] = "src".split(",");//image
  32.   PROP[0][ 4] = "action".split(",");//form
  33.   PROP[0][ 5] = "checked".split(",");//checkbox
  34.   PROP[0][ 6] = "checked".split(",");//radio
  35.   PROP[0][ 7] = "value".split(",");//text
  36.   PROP[0][ 8] = "value".split(",");//textarea
  37.   PROP[0][ 9] = "value".split(",");//password
  38.   PROP[0][10] = "selectedIndex".split(",");//select
  39.  
  40.   //IE 3 Writable Properties
  41.   PROP[1][ 0] = "".split(",");//layer
  42.   PROP[1][ 1] = "".split(",");//div
  43.   PROP[1][ 2] = "".split(",");//span
  44.   PROP[1][ 3] = "".split(",");//image
  45.   PROP[1][ 4] = "action".split(",");//form
  46.   PROP[1][ 5] = "checked".split(",");//checkbox
  47.   PROP[1][ 6] = "checked".split(",");//radio
  48.   PROP[1][ 7] = "value".split(",");//text
  49.   PROP[1][ 8] = "value".split(",");//textarea
  50.   PROP[1][ 9] = "value".split(",");//password
  51.   PROP[1][10] = "selectedIndex".split(",");//select
  52.  
  53.   //Netscape 4 Writable Properties
  54.   PROP[2][ 0] = "top,left,zIndex,clip,visibility,document.bgColor,document.background".split(",");//layer
  55.   PROP[2][ 1] = "".split(",");//div
  56.   PROP[2][ 2] = "".split(",");//span
  57.   PROP[2][ 3] = "src".split(",");//image
  58.   PROP[2][ 4] = "action".split(",");//form
  59.   PROP[2][ 5] = "checked".split(",");//checkbox
  60.   PROP[2][ 6] = "checked".split(",");//radio
  61.   PROP[2][ 7] = "value".split(",");//text
  62.   PROP[2][ 8] = "value".split(",");//textarea
  63.   PROP[2][ 9] = "value".split(",");//password
  64.   PROP[2][10] = "selectedIndex".split(",");//select
  65.  
  66.   //IE 4 Writable Properties
  67.   PROP[3][ 0] = ("style.top,style.left,style.width,style.height,style.zIndex,style.clip,style.visibility,"+
  68.                  "style.backgroundColor,style.backgroundImage,style.filter").split(",");//layer
  69.   PROP[3][ 1] = ("style.fontFamily,style.fontStyle,style.fontWeight,style.fontSize,"+
  70.                 "style.borderStyle,style.borderWidth,style.borderColor,style.backgroundColor,"+
  71.                 "style.backgroundImage,style.filter,innerHTML,innerText").split(",");//div
  72.   PROP[3][ 2] = ("style.fontFamily,style.fontStyle,style.fontWeight,style.fontSize,"+
  73.                 "style.borderStyle,style.borderWidth,style.borderColor,style.backgroundColor,"+
  74.                 "style.backgroundImage,style.filter,innerHTML,innerText").split(",");//span
  75.   PROP[3][ 3] = "src".split(",");//image
  76.   PROP[3][ 4] = "action".split(",");//form
  77.   PROP[3][ 5] = "checked".split(",");//checkbox
  78.   PROP[3][ 6] = "checked".split(",");//radio
  79.   PROP[3][ 7] = "value".split(",");//text
  80.   PROP[3][ 8] = "value".split(",");//textarea
  81.   PROP[3][ 9] = "value".split(",");//password
  82.   PROP[3][10] = "selectedIndex".split(",");//select
  83. }
  84.  
  85. var TAGTYPES;
  86. var MENUMAXLEN;
  87. var PROP;
  88.  
  89.  
  90. //******************* BEHAVIOR FUNCTION **********************
  91.  
  92. //Sets a property of an object to a new value.
  93. //Accepts the following arguments:
  94. //  objName  - simple obj name or Javascript object ref for Netscape (ex: document.layers['foo'].document.myImage)
  95. //  x        - ignored (for backward compatibility)
  96. //  theProp  - the property to change (ex: value, style.fontFace)
  97. //  theValue - the new value (ex: sans-serif)
  98. //Also accepts a simple object name ("image1") as the first argument.
  99. //
  100. //Tests for browser, and uses the first object string for NS, the second for IE
  101. //The next test is to prevent errors in < 4.0 browsers if the object's in a layer.
  102. //If the object still exists, and either it's not a style property, or the style prop is okay.
  103.  
  104. function MM_changeProp(objName,x,theProp,theValue) { //v3.0
  105.   var obj = MM_findObj(objName);
  106.   if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("obj."+theProp+"='"+theValue+"'");
  107. }
  108.  
  109. document.VERSION_MM_changeProp = 3.0; //define latest version number for behavior inspector
  110.  
  111. //******************* API **********************
  112.  
  113.  
  114. //Can be used with any tag and any event
  115.  
  116. function canAcceptBehavior(){
  117.   return true;
  118. }
  119.  
  120.  
  121.  
  122. //Returns a Javascript function to be inserted in HTML head with script tags.
  123.  
  124. function behaviorFunction(){
  125.   return "MM_findObj,MM_changeProp";
  126. }
  127.  
  128.  
  129.  
  130. //Returns fn call to insert in HTML tag <TAG... onEvent='thisFn(arg)'>
  131. //Gets values from the UI and adds them as params to the function call.
  132.  
  133. function applyBehavior() {
  134.   var menuIndex,theObjNS,theProp="",theValue,theTag;
  135.  
  136.   //read all values from UI
  137.   menuIndex = document.theForm.menu.selectedIndex; //get index selected
  138.   if (menuIndex != -1 && document.theForm.menu.options[menuIndex].text.indexOf("*")!=0) {
  139.     theObjNS   = escQuotes(document.MM_NS_REFS[menuIndex]);
  140.     theValue = escQuotes(document.theForm.theValue.value);
  141.     menuIndex = document.theForm.typeOfObj.selectedIndex; //get index selected
  142.     theTag   = document.theForm.typeOfObj.options[menuIndex].text;
  143.  
  144.     if (document.theForm.theRadio[0].checked) { //get property from menu
  145.       theProp = document.theForm.propMenu.options[document.theForm.propMenu.selectedIndex].text;
  146.     } else { //get from textfield
  147.       theProp  = document.theForm.theProp.value;
  148.     }
  149.  
  150.     if (theObjNS.indexOf(REF_UNNAMED) == 0)  //if unnamed reference
  151.       return MSG_UnnamedObj;
  152.     else {
  153.       theObjNS = getNameFromRef(theObjNS);
  154.   } }
  155.  
  156.   if (!theProp) {
  157.     return MSG_NoSelection;
  158.   } else {
  159.     updateBehaviorFns("MM_findObj","MM_changeProp");
  160.     return "MM_changeProp('"+theObjNS+"','','"+theProp+"','"+theValue+"','"+theTag+"')"; //fn call w/args
  161.   }
  162. }
  163.  
  164.  
  165.  
  166. //Returns a dummy function call to inform Dreamweaver the type of certain behavior
  167. //call arguments. This information is used by DW to fixup behavior args when the
  168. //document is moved or changed.
  169. //
  170. //It is passed an actual function call string generated by applyBehavior(), which
  171. //may have a variable list of arguments, and this should return a matching mask.
  172. //
  173. //The return values are:
  174. //  URL     : argument could be a file path, which DW will update during Save As...
  175. //  NS4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  176. //  IE4.0ref: arg is an object ref that may be changed by Convert Tables to Layers
  177. //  other...: argument is ignored (I add a descriptive word for future generations)
  178. //  objName : simple name used by MM_findObj, such as "image1"
  179.  
  180. function identifyBehaviorArguments(fnCallStr) {
  181.   var argArray, retVal = "";
  182.  
  183.   argArray = extractArgs(fnCallStr);
  184.   if (argArray.length == 6)
  185.     retVal = (argArray[i+1].indexOf(".")==-1)? "objName,other,other,other,other" : "NS4.0ref,IE4.0ref,other,other,other";
  186.   return retVal;
  187. }
  188.  
  189.  
  190.  
  191. //Given the original function call, this parses out the args and updates
  192. //the UI.
  193.  
  194. function inspectBehavior(fnCallStr){
  195.   var i,found,numTags,numObjs;
  196.   var theObjNS, theProp, theValue, theTag;
  197.   var argArray = new Array;
  198.  
  199.   //get previous args
  200.   argArray = extractArgs(fnCallStr);
  201.   if (argArray.length > 5) {
  202.     theObjNS = unescQuotes(argArray[1]);
  203.     theProp = argArray[3];
  204.     theValue = unescQuotes(argArray[4]);
  205.     theTag = argArray[5];
  206.  
  207.     //select tag in tag list
  208.     found = false;
  209.     numTags = document.theForm.typeOfObj.options.length;
  210.     for (i=0; i<numTags; i++)  //check if theTag is in menu
  211.       if (document.theForm.typeOfObj.options[i].text == theTag) { //if theTag there
  212.         document.theForm.typeOfObj.selectedIndex = i;
  213.         found = true;
  214.         break;
  215.       }
  216.     if (!found) alert(errMsg(MSG_TagNotFound,theTag));
  217.     else {
  218.       loadAllMenus();  //simulate tag being selected
  219.  
  220.       //select obj in menu
  221.       found = false;
  222.       numObjs = document.MM_NS_REFS.length;
  223.       for (i=0; i<numObjs; i++) { //check if theObjNS is in menu
  224.         if (theObjNS == document.MM_NS_REFS[i] || theObjNS == getNameFromRef(document.MM_NS_REFS[i])) { //if found
  225.           document.theForm.menu.selectedIndex = i;
  226.           found = true;
  227.           break;
  228.       } }
  229.       if (!found) alert(errMsg(MSG_ObjNotFound,theObjNS));
  230.       else {
  231.         document.theForm.theProp.value = theProp;
  232.         selectRadio(1);
  233.         document.theForm.theValue.value = theValue;
  234.       }
  235.     }
  236.  
  237.   }
  238. }
  239.  
  240.  
  241.  
  242. //***************** LOCAL FUNCTIONS  ******************
  243.  
  244.  
  245. //Load the typeOfObj menu with tag names, the browser menu,
  246. //and initialize the object menu.
  247.  
  248. function initializeUI(){
  249.   initGlobals();
  250.   var listLen=eval(TAGTYPES.length); //get array length
  251.  
  252.   //load TAGS picklist
  253.   for (var i=0;i<listLen;i++)
  254.     document.theForm.typeOfObj.options[i]=new Option(TAGTYPES[i]); //load menu
  255.  
  256.   //load browser picklist
  257.   for (i=0;i<BROWSERS.length;i++)
  258.     document.theForm.browserMenu.options[i]=new Option(BROWSERS[i]); //load menu
  259.   document.theForm.browserMenu.selectedIndex = DEFAULT_BROWSER;
  260.  
  261.   document.theForm.propMenu.options[0]=new Option("");
  262.  
  263.   document.theForm.menu.options[0]=new Option("*** "+MENUITEM_NoTypeSelected+" **");
  264.   document.theForm.menu.mylength = 1;  //WORKAROUND! create the length property for main menu
  265.   document.theForm.menu.selectedIndex = 0;
  266.  
  267.   document.theForm.theValue.focus(); //set focus on textbox
  268.   document.theForm.theValue.select(); //set insertion point into textbox
  269. }
  270.  
  271.  
  272.  
  273. //Loads the new objects, and a list of usefule properties.
  274.  
  275. function loadAllMenus() {
  276.   loadObjectMenu();
  277.   loadPropMenu();
  278. }
  279.  
  280.  
  281.  
  282. //Loads a list of useful properties from the data array PROP.
  283.  
  284. function loadPropMenu() {
  285.   var tagIndex, brIndex, i;
  286.  
  287.   //get browser selection from browserMenu
  288.   brIndex = document.theForm.browserMenu.selectedIndex;
  289.  
  290.   //get tag selection from typeOfObj
  291.   tagIndex = document.theForm.typeOfObj.selectedIndex;
  292.  
  293.   if (brIndex > -1 && tagIndex > -1) {
  294.  
  295.     //Can't set menu length, so must erase old values
  296.     for (i=0; i<MENUMAXLEN; i++)
  297.       document.theForm.propMenu.options[i]=new Option("");
  298.  
  299.     //add each property to list
  300.     for (i=0; i<PROP[brIndex][tagIndex].length; i++)
  301.       document.theForm.propMenu.options[i]=new Option(PROP[brIndex][tagIndex][i]);
  302.  
  303.     document.theForm.propMenu.selectedIndex = 0;
  304.     MENUMAXLEN = Math.max(MENUMAXLEN,i);
  305.   }
  306. }
  307.  
  308.  
  309.  
  310. //Load the select menu with object references.
  311.  
  312. function loadObjectMenu() {
  313.   var nameArray = new Array;
  314.   var i,menuLen,tagIndex,tagStr,listLen,niceNameSrcArray;
  315.  
  316.   //put up a temporary msg while their waiting  (not currently seen)
  317.   document.theForm.menu.options[0]=new Option("*** "+MENUITEM_Searching+" ***"); //temporary msg
  318.   document.theForm.menu.selectedIndex = 0; //reselect the menu item
  319.  
  320.   //Clear out old menu (WORKAROUND! Must maintain length field)
  321.   menuLen = document.theForm.menu.mylength;  //get previous length
  322.   for (i=1; i<menuLen; i++) document.theForm.menu.options[i]=new Option(""); //clear each item
  323.   menuLen = 0; //menu now "zero" length
  324.  
  325.   //get list of objects
  326.   tagIndex = document.theForm.typeOfObj.selectedIndex;
  327.   tagStr = document.theForm.typeOfObj.options[tagIndex].text;
  328.   document.MM_NS_REFS = getAllObjectRefs("NS 4.0",tagStr); //store parallel NS refs
  329.   document.MM_IE_REFS = getAllObjectRefs("IE 4.0",tagStr); //store parallel IE refs
  330.   niceNameSrcArray = document.MM_NS_REFS;
  331.  
  332.   //Search for unreferenceable objects. <DIV id="foo"> is IE only, <LAYER> is NS only.
  333.   //if REF_CANNOT found, return empty string, and use IE refs for nice namelist.
  334.   for (i=0; i<document.MM_NS_REFS.length; i++) {
  335.     if (document.MM_IE_REFS[i].indexOf(REF_CANNOT) == 0) {
  336.       document.MM_IE_REFS[i] = ""; //blank it out
  337.     }
  338.     if (document.MM_NS_REFS[i].indexOf(REF_CANNOT) == 0) {
  339.       document.MM_NS_REFS[i] = ""; //blank it out
  340.       niceNameSrcArray = document.MM_IE_REFS; //use the IE list
  341.     }
  342.   }
  343.   nameArray = niceNames(niceNameSrcArray,TAGNAMES[tagIndex]);
  344.  
  345.   //load menu with object names
  346.   if (nameArray.length == 0) {  //if nothing to display...
  347.     document.theForm.menu.options[0]=new Option("*** "+errMsg(MENUITEM_ItemsNotFnd,tagStr)+" ***"); //clear menu
  348.   } else { //something there, load the menu
  349.     listLen = nameArray.length; //get array length
  350.     for (menuLen=0; menuLen<listLen; menuLen++)
  351.       document.theForm.menu.options[menuLen]=new Option(nameArray[menuLen]); //load menu
  352.   }
  353.   document.theForm.menu.mylength = menuLen;  //WORKAROUND! re-store the length property
  354.  
  355.   document.theForm.typeOfObj.selectedIndex = tagIndex; //reselect the menu item
  356. }
  357.  
  358.  
  359.  
  360. //Passed a number, selects that radio.
  361.  
  362. function selectRadio(num) {
  363.   document.theForm.theRadio[0].checked = (num==0)?true:false;
  364.   document.theForm.theRadio[1].checked = (num==1)?true:false;
  365. }
  366.